<?php
    $form = $this->form;
    $form->prepare ();    
    $form->setAttribute ( 'method', 'post' );    

    $option = array ();
    if(isset($this->eventList))
    {
        $form->setAttribute ( 'action', $this->url ( NULL, array (
            'controller' => 'Register',
            'action' => 'process'
        ) ) );
        
        $eventList = $this->eventList;
        $value = 0;
        foreach ( $eventList as $event ) { 
        	$value = $this->escapeHtml ( $event->TITLE ); // 'name' name of the column in the database
        	$id = $this->escapeHtml ( $event->ID );
        	$option [] = array (
        			'value' => $id,
        			'label' => $value 
        	);
        }
    }elseif(isset($this->event)){
        
        $form->setAttribute ( 'action', $this->url ('events', array (
            'controller' => 'EventParticipant',
            'action' => 'process',
            'id' => $this->event->ID            
        ) ) );
        
            $option [] = array (
        			'value' => $this->event->ID,
        			'label' => $this->event->TITLE 
        	);
    }
    $form->get ( 'PENDINGEVENT' )->setAttributes ( array (
    		'options' => $option 
    ) );    
?>


<div class="container">
	<div class="row">
		<div class="col-md-4">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">Event</h3>
				</div>

				<div class="panel-body">
				<?php echo $this->form()->openTag($form); ?>
				<?php
    				echo $this->formelement ( $form->get ( 'ID' ) );
				?> 
				
                    <div class="form-group">
						<label class="col-md-3 control-label"><?php echo "Title:"?></label>
						<div class="col-md-9">							
						  <?php	echo $this->formelement ( $form->get ( 'PENDINGEVENT' ) );?>
						  <span class="errormsgform"><?php echo $this->formElementErrors($form->get('PENDINGEVENT')); ?></span>
						</div>
					</div>

					<div class="form-group">
						<label class="col-md-3 control-label"><?php echo "Subject:"?></label>
						<div class="col-md-9">
							<div id="data-title" class="form-control">Empty</div>
						</div>
					</div>

					<div class="form-group">
						<label class="col-md-3 control-label"><?php echo "Place:"?></label>
						<div class="col-md-9">
							<div id="data-place" class="form-control">Empty</div>
						</div>
					</div>

					<div class="form-group">
						<label class="col-md-3 control-label"><?php echo "Date:"?></label>
						<div class="col-md-9">
							<div id="data-date" class="form-control">Empty</div>
						</div>
					</div>

				</div>
			</div>
		</div>


		<div class="col-md-7">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">Register Information</h3>
				</div>

				<div class="panel-body" id="register-form">
					<div class="form-horizontal">

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('PHONE'));?></label>
							<div class="col-md-7">
                            <?php echo $this->formElement($form->get('PHONE'));?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('PHONE')); ?></span>
							</div>
							<div class="col-md-2 left">
                            <?php // echo $this->formElement($form->get('BTNADDPHONE'));?>
                        </div>
						</div>
						
						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('EMAIL'))?></label>
							<div class="col-md-7">
                            <?php echo $this->formElement($form->get('EMAIL')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('EMAIL')); ?></span>
							</div>
							<div class="col-md-2">
                        <?php // echo $this->formelement($form->get('BTNADDEMAIL'));?>
                        </div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('TITLE'));?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('TITLE')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('TITLE')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('FIRST_NAME'));?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('FIRST_NAME')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('FIRST_NAME')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('MIDDLE_NAME'))?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('MIDDLE_NAME')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('MIDDLE_NAME')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('LAST_NAME'))?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('LAST_NAME')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('LAST_NAME')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('GENDER'))?></label>
							<div class="col-md-9"> 
                            <?php echo $this->formElement($form->get('GENDER')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('GENDER')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('ADDRESS'))?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('ADDRESS')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('ADDRESS')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('FUNDING'))?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('FUNDING')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('FUNDING')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"><?php echo $this->formlabel($form->get('ORGANIZATION'))?></label>
							<div class="col-md-9">
                            <?php echo $this->formElement($form->get('ORGANIZATION')); ?>
                            <span class="errormsgform"><?php echo $this->formElementErrors($form->get('ORGANIZATION')); ?></span>
							</div>
						</div>

						<div class="form-group">
							<label class="col-md-3 control-label"></label>
							<div class="col-md-9">
                            <?php
    							echo $this->formElement ( $form->get ( 'BTNADD' ) );
    							echo " ";?>
    							<a href="/register"><?php echo $this->formElement ( $form->get ( 'BTNCANCEL' ) );?></a>
							
                        </div>
						</div>
                  
                <?php echo $this->form()->closeTag();?>
                </div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
    $(document).ready(function() {  
    	guiInit();
    	ajaxGetEventData();
    	searchByPhone();
    	errorMsgRemover();    	   	
    });

   checkEvent=function(data){

        var isPresent=false;

        var objs=JSON.parse(data.EVENTS);
	    jQuery.each(objs, function(i, obj) {
	    	  if($("#pendingEvent").find('option:selected').attr('value') == obj.FK_EVENT_ID){
		    	  isPresent=true;
	    	  }
	    });
    	
    	return isPresent;
    };
    
    searchByPhone=function(){
    	$("#PHONE,#pendingEvent").bind('select change',function() {
    		$("#BTNADD").val("Add");
    		$("#BTNADD").attr("disabled",false);
    		$("#error-msg").remove();
    		
        	clearParticipantData();
    		$phone=$("#PHONE").val();
    		$.ajax({
    			method: "POST",
    			url: "<?php echo $this->url('register', array('controller'=>'Index','action'=>'getParticipantData')); ?>",
    			data: { textData:$phone }
    		})
    		.done(function( data ) {
        		
        			$('#ID').val(data.ID);
        			$('#EMAIL').val(data.EMAIL);
        			$('#TITLE').val(data.TITLE);

        			$('input[name=GENDER]:checked').val(data.GENDER);
        			if(data.GENDER == 2){
        			    $("input[type='radio'][name=GENDER][value=2]").attr('checked',true);
        			    $("input[type='radio'][name=GENDER][value=1]").attr('checked',false);
        			}else{
        				$("input[type='radio'][name=GENDER][value=1]").attr('checked',true);
        				$("input[type='radio'][name=GENDER][value=2]").attr('checked',false);	
        			}
            		$('#FIRST_NAME').val(data.FIRST_NAME);
            		$('#MIDDLE_NAME').val(data.MIDDLE_NAME);
            		$('#LAST_NAME').val(data.LAST_NAME);
            		$('#ADDRESS').val(data.ADDRESS);
            		/* $('input[name=FUNDING]:checked').val(data.FUNDING);
            		
            		if(data.FUNDING == 2){
        			    $("input[type='radio'][name=FUNDING][value=2]").attr('checked',true);
        			    $("input[type='radio'][name=FUNDING][value=1]").attr('checked',false);
        			}else{
        				$("input[type='radio'][name=FUNDING][value=1]").attr('checked',true);
        				$("input[type='radio'][name=FUNDING][value=2]").attr('checked',false);	
        			} */
            		//$('#ORGANIZATION').val(data.ORGANIZATION);
            		
            		if(!data.ID ==""){
            	    	errorMsgRemoverAll(); 
            		    $("#BTNADD").val("Add to Event");            		    
            		    if(checkEvent(data)){
            		    	$("#BTNADD").attr('disabled',true);
            		    	$("#register-form").append('<p id="error-msg" class="error">Participant already registered</p>');
            		    }
            		}
    		});
        });
    	clearParticipantData=(function(){
    		$('#ID').val("");
    		$('#EMAIL').val("");
			$('#TITLE').val("");
			$('#GENDER').val("");
    		$('#FIRST_NAME').val("");
    		$('#MIDDLE_NAME').val("");
    		$('#LAST_NAME').val("");
    		$('#ADDRESS').val("");
    		$('#FUNDING').val("");
    		$('#ORGANIZATION').html("");    		
    	});
    	
    };

    guiInit=function(){
    	$("#ORGANIZATION").attr("disabled", true); 
    	$("input[name=FUNDING]").click(function() {
    	    $("#ORGANIZATION").attr("disabled", false);
    	    if ($("input[name=FUNDING]:checked").val() == "1") {
    	        $("#ORGANIZATION").attr("disabled", true);
    	        $("#ORGANIZATION").val("");
    	    }
    	});
  
    };
    
    ajaxGetEventData=function(){

    	$("#pendingEvent").change(function() {
        	clearEventData();
    		$id=$("#pendingEvent").val();
    		$.ajax({
    			method: "POST",
    			url: "<?php echo $this->url('register', array('controller'=>'Index','action'=>'rolecombo')); ?>",
    			data: { textData:$id }
    		})
    		.done(function( data ) {
        		$('#data-title').html(data.title);     		  
        		$('#data-place').html(data.place);
        		$('#data-date').html(data.date);
    		});
    		
    	});

    	clearEventData = function(){
    		$('#data-title').html("");
    		$('#data-place').html("");
    		$('#data-date').html("");
    	};
    };

    errorMsgRemover=function(){
    	$('.form-control').bind('select click keyup checked',function() {
        	$(this).siblings('.errormsgform').html('');
    	}); 
    	$("input[name=GENDER]").click(function() {
    	    $(this).parents('label').siblings('.errormsgform').html('');
    	});
    	$("input[name=FUNDING]").click(function() {
    	    $(this).parents('label').siblings('.errormsgform').html('');
    	});
    };
    errorMsgRemoverAll=function(){
    
        	$('.form-control').siblings('.errormsgform').html(''); 
    	    $("input[name=GENDER]").parents('label').siblings('.errormsgform').html('');
    	    $("input[name=FUNDING]").parents('label').siblings('.errormsgform').html('');
    };
</script>
<?php $flashMessages=$this->flashMessages?>
<?php if(count($flashMessages)) : ?>
    <?php foreach ($flashMessages as $msg) : ?>
    <?php echo "<script type=\"text/javascript\">alert('$msg');</script>"; ?>
    <?php endforeach; ?>
<?php endif; ?>